package Compiler;

import java.io.*;
import java.util.*;


public class Compiler{
	// inits
	// global variable for the current Token
	public static String Token;
	static Lexical Lexer;
	static Syntax parser;
	static Semantic seman;
	private static LinkedList<String> list1 = new LinkedList<String>();
    private static LinkedList list2 = new LinkedList();
    
    public static void main(String[] args) throws IOException {

    	 Token = "";
         String fileName = args[0];

         //checks if there is only one 1 file
         if(args.length != 1) {
                 System.err.println("ERROR: more than one file found");
                 System.exit(0);                 
         }
         
         if(!fileName.endsWith(".lol"))
         {
                 System.err.println("ERROR: file is not in '.lol' format");
                 System.exit(0);                     
         }
         
         
         
         try{    
             Lexer = new Lexical(fileName);
         }
         catch(FileNotFoundException ex) 
         {
             System.out.println("File not found.");      
         }
         
         
         Token = Lexer.getNextToken();

         parser = new Syntax();
         parser.lollist1(list1);

         seman = new Semantic(); // still need to add this
         list1 = seman.translate(list1);


         Writer writer = null;
         try 
         {
             writer = new BufferedWriter(new OutputStreamWriter(
             new FileOutputStream("output.html"), "utf-8"));
             printlist1(writer, list1);
         } catch (IOException ex) {
             System.out.println("Error printing file");
         } finally {
            try {writer.close();} catch (Exception ex) {}
         }
         
         Lexer.close();
         
 }
  
/**
*
* @param writer
* @param list1
* @throws IOException
*/
public static void printlist1(Writer writer, LinkedList list1) throws IOException
{   
 String head;
 for(int i=0; i<list1.size(); i++)
 {
     head = (String)list1.get(i);
     //System.out.println(head);
     writer.write(head + "\n");            
 }
}
 
}
